package com.shxc.smarttrade.old.modal;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.old.bean.OpenTrader;
import com.shxc.smarttrade.old.bean.PendingOrder;
import com.shxc.smarttrade.old.bean.StockChartDatas;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;

import org.xchris.x;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/10/27.
 */

public class SimpleTradeModal implements ISimpleTrade {
    /**
     * 获取K线图数据
     *
     * @param accessToken
     * @param symbol
     * @param type
     * @param listener
     */
    @Override
    public void getStockChartData(String accessToken, String symbol, String type, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("symbol", symbol);
        map.put("type", type);
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.STOCK_CHART_ACTIVITY_HISTORYQUOTE, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                Gson gson = new Gson();
                StockChartDatas scd = gson.fromJson(response, StockChartDatas.class);
                if (scd.getErr_code().equals("0"))
                    listener.success(scd);
                else
                    listener.faild(scd.getErr_msg());
            }
        });

    }

    /**
     * open
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     * @param listener
     */
    @Override
    public void wareHouse(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("symbol", symbol);
        map.put("userId", userId + "");
        map.put("cmd", cmd + "");
        map.put("volume", volume + "");
        map.put("tp", tp + "");
        map.put("sl", sl + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.STOCK_CHART_ACTIVITY_OPEN, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");

            }

            @Override
            public void success(String response, int id) {
//                Log.e("response..", response);
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        if (jsonObject.get("data").isJsonObject()) {
                            JsonObject data = jsonObject.get("data").getAsJsonObject();
                            if (data.get("openTrader").isJsonObject()) {
                                JsonObject openTrader = data.get("openTrader").getAsJsonObject();
                                Gson gson = new Gson();
                                OpenTrader openTrader1 = gson.fromJson(openTrader, OpenTrader.class);
                                listener.success(openTrader1);
                            } else {
                                listener.faild("数据异常");
                            }
                        } else {
                            listener.faild("数据异常");
                        }
                    } else {
                        listener.faild(errMsg);
                    }

                } else {
                    listener.faild("数据异常");
                }
            }
        });
    }

    /**
     * 挂单
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     * @param pendingPrice
     * @param market
     * @param listener
     */
    @Override
    public void pengingOrder(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl, final double pendingPrice, final double market, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("symbol", symbol);
        map.put("userId", userId + "");
        map.put("cmd", cmd + "");
        map.put("volume", volume + "");
        map.put("tp", tp + "");
        map.put("sl", sl + "");
        map.put("pendingPrice", pendingPrice + "");
        map.put("market", market + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.STOCK_CHART_ACTIVITY_PENDING_ORDER, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");

            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        if (!jsonObject.get("data").isJsonNull()) {
                            JsonObject data = jsonObject.get("data").getAsJsonObject();
                            if (!data.get("pendingOrder").isJsonNull()) {
                                JsonObject openTrader = data.get("pendingOrder").getAsJsonObject();
                                Gson gson = new Gson();
                                PendingOrder pendingOrder = gson.fromJson(openTrader, PendingOrder.class);
                                listener.success(pendingOrder);
                            } else {
                                listener.faild("数据异常");
                            }
                        } else {
                            listener.faild("数据异常");
                        }

                    } else {
                        listener.faild(errMsg);
                    }
                } else {
                    listener.faild("数据异常");
                }
            }
        });
    }

}
